package reflect;

import java.util.Scanner;

/**
 * @author liyang
 * @version 1.0
 * @package reflect
 * @file ReflectDemo2.java
 * @description
 * @time 2024/6/23 下午6:23
 */
public class ReflectDemo2 {
    public static void main(String[] args) throws Exception {
        /*正常的实例化过程：*/
        Person person = new Person();
        System.out.println(person);
        /*用反射机制实现 实例化 操作：*/
        /*注意：需要有无参构造器才行*/
        Scanner scanner = new Scanner(System.in);
        System.out.print("输入一个要实例化的类名: ");
        String className = scanner.nextLine();

        Class cls = Class.forName(className);
        Object obj = cls.newInstance();
        System.out.println(obj);
    }
}
